﻿//wrapper
var chirper = {};
chirper.base = "9er";
chirper.cage_Chirp = [];
chirper.cage_Profile = [];
chirper.cage_Friend = [];
chirper.cage_ChirpF = [];
// constructor for chirps
chirper.Chirp = function (name, message, timestamp) {
    this.name = name;
    this.message = message;
    this.timestamp = timestamp;
};
chirper.Chirp.prototype.isFlagged = false;
// constructor for profiles
chirper.Profile = function (name, image, bio) {
    this.name = name;
    this.image = image;
    this.bio = bio;
};
chirper.Profile.prototype.isUpdated = false;
// constructor for friends
chirper.Friend = function (name, domain) {
    this.name = name;
    this.domain = domain;
};
chirper.Friend.prototype.isFriend = false;
chirper.urlHelper = function (base) {
    var url = "https://" + base + ".firebaseio.com/"
    for (var i = 1; i < arguments.length; i++) {
        url += arguments[i] + "/"
    }
    return url + ".json";
}
/************************************************* Chirp functions *******************************/
chirper.drawTable_Chirp = function () {
    var h = "";
    //h += "";
    for (var i in chirper.cage_Chirp) {
        var currentChirp = chirper.cage_Chirp[i];
        h += "<div style='text-align: center'>";
        h += "<h3>";
        h += "<i class='fa fa-book'></i>  ";
        h += currentChirp.name + "</h3><br/>";
        h += "<h4>" + currentChirp.message + "</h4><br/>";
        h += "<h4>" + currentChirp.timestamp + "</h4><br/>";
        h += "<div class='btn btn-danger fa fa-recycle' onclick='chirper.delete_Chirp(" + i + ")'></div>";
        h += "<div class='btn btn-warning fa fa-edit' onclick='chirper.showEdit_Chirp(" + i + ")'></div>"
        h += "</div>";
    }
    h += "</div>";
    h += "</div>";
    document.getElementById("chirpHolder").innerHTML = h;
};


chirper.addMessage_Chirp = function () {
    "use strict"
    
    var timestamp = Date.now();
    var message, name;
    name = document.getElementById("name");
    message = document.getElementById("message");
    var s = new chirper.Chirp(name.value, message.value, timestamp)
    chirper.cage_Chirp.push(s);
    name.value = "";
    message.value = "";
    timestamp.value = "";
    chirper.ajax("POST", chirper.urlHelper(chirper.base, "chirps"), s, function () {
        chirper.read_Chirp();
    //chirper.drawTable_Chirp();
    });

};

chirper.read_Chirp = function () {
    chirper.cage_Chirp = [];
    chirper.ajax("GET", chirper.urlHelper(chirper.base, "chirps"), null, function (data) {
        for (var i in data) {
            var chirp = new chirper.Chirp(data[i].name, data[i].message, data[i].timestamp); // parameters
            chirp.key = i;
            chirper.cage_Chirp.push(chirp);
        }
        chirper.drawTable_Chirp();
    })
};

//Delete Message
chirper.delete_Chirp = function (index) {
    chirper.ajax("DELETE", chirper.urlHelper(chirper.base, "chirps", chirper.cage_Chirp[index].key), null, function () {chirper.read_Chirp();
        });
    setTimeout(function () { chirper.cage_Chirp.splice(index, 1); }, 2000);
    chirper.drawTable_Chirp();
}
//Show Edit
chirper.showEdit_Chirp = function (index) {
    var chirp = chirper.cage_Chirp[index];
    //document.getElementById("editTitle").innerHTML = "Message Edit" + chirp.name;
    document.getElementById("editName").value = chirp.name;
    document.getElementById("editMessage").value = chirp.message;
    document.getElementById("isFlagged").value = chirp.isFlagged.toString();
    document.getElementById("saveChangesBtn").onclick = function () { chirper.saveEdit_Chirp(index); };
    $("#editModal").modal();

};
// Save Edit
chirper.saveEdit_Chirp = function (index) {
    var chirp = chirper.cage_Chirp[index];
    chirp.name = document.getElementById("editName").value;
    chirp.message = document.getElementById("editMessage").value;
    if (document.getElementById("isFlagged").value === "true") {
        chirp.isFlagged = true;
    }
    else {
        delete chirp.isFlagged;
    }
    var s = new chirper.Chirp(chirp.name, chirp.message);
    $("#editModal").modal("hide");
    chirper.drawTable_Chirp();
    chirper.ajax("PATCH", chirper.urlHelper(chirper.base, "chirps", chirper.cage_Chirp[index].key), s, function () {
        chirper.read_Chirp();
    });
};

/**************** AJAX *****************/
chirper.ajax = function (method, url, data, success, error) {
    var request = new XMLHttpRequest();
    request.open(method, url);
    request.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            success(JSON.parse(this.response));
        } else {
            console.log("Error on " + method);
            error();
        }
    };
    request.onerror = function () {
        console.log("Communication error");
        error();
    }
    if (data) {
        request.send(JSON.stringify(data));
    } else {
        request.send();
    }
};
/*****************************************/
//Clear All
chirper.clearAll_Chirp = function () {
    chirper.cage_Chirp = [];
    chirper.drawTable_Chirp();
}
//Seeder
chirper.read_Chirp();

/************************************************* Profile functions *******************************/

chirper.drawTable_Profile = function () {
    var h = "";
    for (var i in chirper.cage_Profile) {
        var currentChirp = chirper.cage_Profile[i];
        h += "<div style='text-align: center'>";
        h += "<h4>" + currentChirp.name + "</h4><br/>";
        h += "<img src='" + currentChirp.image + "' class='img-responsive'/><br/>";
        h += "<h4>" + currentChirp.bio + "</h4><br/>";
        h += "<div class='btn btn-danger fa fa-recycle' onclick='chirper.delete_Profile(" + i + ")'></div>";
        h += "<div class='btn btn-warning fa fa-edit' onclick='chirper.showEdit_Profile(" + i + ")'></div>";
    }
    h += "</div>";
    document.getElementById("profileHolder").innerHTML = h;
};


chirper.addMessage_Profile = function () {
    "use strict"
    var name, image, bio;
    name = document.getElementById("name");
    image = document.getElementById("image");
    bio = document.getElementById("bio");
    var s = new chirper.Profile(name.value, image.value, bio.value)
    chirper.cage_Profile.push(s);
    image.value = "";
    name.value = "";
    bio.value = "";
    chirper.ajax("POST", chirper.urlHelper(chirper.base, "profile"), s, function () {
        chirper.read_Profile();
        //chirper.drawTable_Profile();
    });

};
chirper.read_ProfileAllChirps = function () {
    
    var allChirps;
    allChirps = document.getElementById("base").value;
    chirper.ajax("GET", chirper.urlHelper(allChirps, "chirps"), null, function (data) { // get chirp data from friends servers
        console.log(data);
        for (var i in data) {
            var chirp = new chirper.Chirp(data[i].name, data[i].message, data[i].timestamp); // parameters
            chirp.key = i;
            chirper.cage_ChirpF.push(chirp); // separate array to store friend chirps
            //chirper.ajax("POST", chirper.urlHelper(chirper.base, "friends"), chirp, function (data) { }); // push data to your server
        }
        chirper.drawTable_ChirpF(); // draw under profile
    })
};

chirper.drawTable_ChirpF = function () {
    var f = "";
    for (var i in chirper.cage_ChirpF) {
        var currentChirpF = chirper.cage_ChirpF[i];
        f += "<div style='text-align: center'>";
        f += "<h3>";
        f += "<i class='fa fa-book'></i>  ";
        f += currentChirpF.name + "</h3><br/>";
        f += "<h4>" + currentChirpF.message + "</h4><br/>";
        f += "<h4>" + currentChirpF.timestamp + "</h4><br/>";
        f += "<div class='btn btn-danger fa fa-recycle' onclick='chirper.delete_Chirp(" + i + ")'></div>";
        f += "<div class='btn btn-warning fa fa-edit' onclick='chirper.showEdit_Chirp(" + i + ")'></div>"
        f += "</div>";
    }
    f += "</div>";
    f += "</div>";
    document.getElementById("chirpHolderF").innerHTML = f;
};

chirper.read_Profile = function () {
    chirper.cage_Profile = [];
    chirper.ajax("GET", chirper.urlHelper(chirper.base, "profile"), null, function (data) {
        for (var i in data) {
            var chirp = new chirper.Profile(data[i].name, data[i].image, data[i].bio); // parameters
            chirp.key = i;
            chirper.cage_Profile.push(chirp);
        }
        chirper.drawTable_Profile();
    })
};

//Delete Message
chirper.delete_Profile = function (index) {
    chirper.ajax("DELETE", chirper.urlHelper(chirper.base, "profile", chirper.cage_Profile[index].key), null, function () {
        chirper.read_Profile();
    });
    setTimeout(function () { chirper.cage_Profile.splice(index, 1); }, 2000);
    chirper.drawTable_Profile();
}
//Show Edit
chirper.showEdit_Profile = function (index) {
    var chirp = chirper.cage_Profile[index];
    //document.getElementById("editTitle").innerHTML = "Message Edit" + chirp.name;
    document.getElementById("editNameP").value = chirp.name;
    document.getElementById("editImage").value = chirp.image;
    document.getElementById("editBio").value = chirp.bio;
    document.getElementById("isUpdated").value = chirp.isUpdated.toString();
    document.getElementById("saveChangesBtnP").onclick = function () { chirper.saveEdit_Profile(index); };
    $("#editModalP").modal();

};
// Save Edit
chirper.saveEdit_Profile = function (index) {
    var chirp = chirper.cage_Profile[index];
    chirp.name = document.getElementById("editNameP").value;
    chirp.image = document.getElementById("editImage").value;
    chirp.bio = document.getElementById("editBio").value;
    if (document.getElementById("isUpdated").value === "true") {
        chirp.isUpdated = true;
    }
    else {
        delete chirp.isUpdated;
    }
    var s = new chirper.Profile(chirp.name, chirp.image, chirp.bio);
    $("#editModalP").modal("hide");
    chirper.drawTable_Profile();
    chirper.ajax("PATCH", chirper.urlHelper(chirper.base, "profile", chirper.cage_Profile[index].key), s, function () {
        chirper.read_Profile();
    });
};

//Clear All
chirper.clearAll_Chirp = function () {
    chirper.cage_Profile = [];
    chirper.drawTable_Profile();
}
//Seeder
chirper.read_Profile();

/**********************************Friends Function *****************************************/

// constructor for friends
chirper.Friend = function (name, image, base) {
    this.name = name;
    this.image = image;
    this.base = base;
};
chirper.Friend.prototype.isFriend = false;
chirper.urlHelper = function (base) {
    var url = "https://" + base + ".firebaseio.com/"
    for (var i = 1; i < arguments.length; i++) {
        url += arguments[i] + "/"
    }
    return url + ".json";
}
chirper.drawTable_Friend = function () {
    var h = "<p style='text-align: center'><th><h3>Friends</h3></p>";
    for (var i in chirper.cage_Friend) {
        var currentFriend = chirper.cage_Friend[i];
        h += "<p style='text-align: center'><h4>" + currentFriend.name + "</h4></p>";
        h += "<img src='" + currentFriend.image + "' class='img-responsive'/><br/>";
        h += "<p><h4>Base: " + currentFriend.base + "</h4></p>";
        h += "<i class='btn btn-danger fa fa-recycle' onclick='chirper.delete_Friend(" + i + ")'></i>";
        h += "<i class='btn btn-warning fa fa-edit' onclick='chirper.showEdit_Friend(" + i + ")'></i>";
    }
    document.getElementById("friendHolder").innerHTML = h;
};

chirper.read_Friend = function () {
    var base;
    base = document.getElementById("base").value;
    chirper.ajax("GET", chirper.urlHelper(base, "profile"), null, function (data) { // get data from friends servers
        console.log(data);
        for (var i in data) {
            var chirp = new chirper.Friend(data[i].name, data[i].image, base); // parameters
            chirp.key = i;
            chirper.cage_Friend.push(chirp);
            chirper.ajax("POST", chirper.urlHelper(chirper.base, "friends"), chirp, function (data) { }); // push data to your server


        }
        chirper.drawTable_Friend();
    })
};
//Delete Message
chirper.delete_Friend = function (index) {
    //chirper.ajax("DELETE", chirper.urlHelper(chirper.base, "friends", chirper.cage_Friend[index].key), null, function () {
    // chirper.read_Friend();
    // });
    // setTimeout(function () { chirper.cage_Friend.splice(index, 1); }, 2000);
    chirper.cage_Friend.splice(index, 1);
    chirper.drawTable_Friend();
}
//Show Edit
chirper.showEdit_Friend = function (index) {
    var chirp = chirper.cage_Friend[index];
    //document.getElementById("editTitle").innerHTML = "Message Edit" + chirp.name;
    document.getElementById("editNameF").value = chirp.name;
    document.getElementById("editImage").value = chirp.image;
    document.getElementById("editDomain").value = chirp.base;
    //document.getElementById("isFriend").value = chirp.isFriend.toString();
    document.getElementById("saveChangesBtnF").onclick = function () { chirper.saveEdit_Friend(index); };
    $("#editModalF").modal();

};
// Save Edit
chirper.saveEdit_Friend = function (index) {
    var chirp = chirper.cage_Friend[index];
    chirp.name = document.getElementById("editNameF").value;
    chirp.image = document.getElementById("editImage").value;
    chirp.base = document.getElementById("editDomain").value;
    //if (document.getElementById("isFriend").value === "true") {
    //    chirp.isFriend = true;
    //}
    //else {
    //    delete chirp.isFriend;
    //}
    var s = new chirper.Friend(chirp.name, chirp.image, chirp.bio);
    $("#editModalF").modal("hide");
    chirper.drawTable_Friend();
    chirper.ajax("PATCH", chirper.urlHelper(chirper.base, "friends", chirper.cage_Friend[index].key), s, function () {
        chirper.read_Friend();
    });
};

//Clear All
chirper.clearAll_Chirp = function () {
    chirper.cage_Friend = [];
    chirper.drawTable_Friend();
}
//Seeder
//chirper.drawTable_Friend();

